﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;

using HsTec.Mobile.Green.Util;

namespace HsTec.Mobile.Green.DAL
{
    /// <summary>
    /// 数据访问类Benchmark。
    /// </summary>
    public class BenchmarkDAL
    {
        public BenchmarkDAL()
        { }
        #region  成员方法
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string sExistsWhere)
        {
            return SQLHelper.Exists("tb_Benchmark", sExistsWhere);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(HsTec.Mobile.Green.Model.Benchmark model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into tb_Benchmark(");
            strSql.Append("TotalProportion,LastMonthlProportion,CumulativProportion,AdjustProportion,ExpectedRate,ActualRate,CalculationBenchmark,ManuallyBenchmark)");
            strSql.Append(" values (");
            strSql.Append("@TotalProportion,@LastMonthlProportion,@CumulativProportion,@AdjustProportion,@ExpectedRate,@ActualRate,@CalculationBenchmark,@ManuallyBenchmark)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@TotalProportion", SqlDbType.NVarChar,50),
					new SqlParameter("@LastMonthlProportion", SqlDbType.NVarChar,50),
					new SqlParameter("@CumulativProportion", SqlDbType.NVarChar,50),
					new SqlParameter("@AdjustProportion", SqlDbType.NVarChar,50),
					new SqlParameter("@ExpectedRate", SqlDbType.NVarChar,50),
					new SqlParameter("@ActualRate", SqlDbType.NVarChar,50),
					new SqlParameter("@CalculationBenchmark", SqlDbType.NVarChar,50),
					new SqlParameter("@ManuallyBenchmark", SqlDbType.NVarChar,50)};
            parameters[0].Value = model.TotalProportion;
            parameters[1].Value = model.LastMonthlProportion;
            parameters[2].Value = model.CumulativProportion;
            parameters[3].Value = model.AdjustProportion;
            parameters[4].Value = model.ExpectedRate;
            parameters[5].Value = model.ActualRate;
            parameters[6].Value = model.CalculationBenchmark;
            parameters[7].Value = model.ManuallyBenchmark;

            return SQLHelper.ExecuteNonQuery(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public int Update(HsTec.Mobile.Green.Model.Benchmark model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update tb_Benchmark set ");
            strSql.Append("TotalProportion=@TotalProportion,");
            strSql.Append("LastMonthlProportion=@LastMonthlProportion,");
            strSql.Append("CumulativProportion=@CumulativProportion,");
            strSql.Append("AdjustProportion=@AdjustProportion,");
            strSql.Append("ExpectedRate=@ExpectedRate,");
            strSql.Append("ActualRate=@ActualRate,");
            strSql.Append("CalculationBenchmark=@CalculationBenchmark,");
            strSql.Append("ManuallyBenchmark=@ManuallyBenchmark");
            strSql.Append(" where BM_ID=@BM_ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@BM_ID", SqlDbType.Int,4),
					new SqlParameter("@TotalProportion", SqlDbType.NVarChar,50),
					new SqlParameter("@LastMonthlProportion", SqlDbType.NVarChar,50),
					new SqlParameter("@CumulativProportion", SqlDbType.NVarChar,50),
					new SqlParameter("@AdjustProportion", SqlDbType.NVarChar,50),
					new SqlParameter("@ExpectedRate", SqlDbType.NVarChar,50),
					new SqlParameter("@ActualRate", SqlDbType.NVarChar,50),
					new SqlParameter("@CalculationBenchmark", SqlDbType.NVarChar,50),
					new SqlParameter("@ManuallyBenchmark", SqlDbType.NVarChar,50)};
            parameters[0].Value = model.BM_ID;
            parameters[1].Value = model.TotalProportion;
            parameters[2].Value = model.LastMonthlProportion;
            parameters[3].Value = model.CumulativProportion;
            parameters[4].Value = model.AdjustProportion;
            parameters[5].Value = model.ExpectedRate;
            parameters[6].Value = model.ActualRate;
            parameters[7].Value = model.CalculationBenchmark;
            parameters[8].Value = model.ManuallyBenchmark;

            return SQLHelper.ExecuteNonQuery(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public int Delete(string sIDs)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tb_Benchmark ");
            strSql.Append(" where BM_ID in ( " + sIDs + " )");
            return SQLHelper.ExecuteNonQuery(strSql.ToString());
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public HsTec.Mobile.Green.Model.Benchmark GetModel(int BM_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 BM_ID,TotalProportion,LastMonthlProportion,CumulativProportion,AdjustProportion,ExpectedRate,ActualRate,CalculationBenchmark,ManuallyBenchmark from tb_Benchmark ");
            strSql.Append(" where BM_ID=@BM_ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@BM_ID", SqlDbType.Int,4)};
            parameters[0].Value = BM_ID;

            HsTec.Mobile.Green.Model.Benchmark model = new HsTec.Mobile.Green.Model.Benchmark();
            DataSet ds = SQLHelper.ExecuteDataset(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["BM_ID"].ToString() != "")
                {
                    model.BM_ID = int.Parse(ds.Tables[0].Rows[0]["BM_ID"].ToString());
                }
                model.TotalProportion = ds.Tables[0].Rows[0]["TotalProportion"].ToString();
                model.LastMonthlProportion = ds.Tables[0].Rows[0]["LastMonthlProportion"].ToString();
                model.CumulativProportion = ds.Tables[0].Rows[0]["CumulativProportion"].ToString();
                model.AdjustProportion = ds.Tables[0].Rows[0]["AdjustProportion"].ToString();
                model.ExpectedRate = ds.Tables[0].Rows[0]["ExpectedRate"].ToString();
                model.ActualRate = ds.Tables[0].Rows[0]["ActualRate"].ToString();
                model.CalculationBenchmark = ds.Tables[0].Rows[0]["CalculationBenchmark"].ToString();
                model.ManuallyBenchmark = ds.Tables[0].Rows[0]["ManuallyBenchmark"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataView GetBenchmarkList(string years, ref string sErr)
        {
            string sLastYears = DateTime.Parse(years.Substring(0, 4) + "-" + years.Substring(4, 2) + "-01").AddYears(-1).ToString("yyyyMM");
            string sYear = years.Substring(0, 4);
            string sLastYearsMonth = DateTime.Parse(years.Substring(0, 4) + "-" + years.Substring(4, 2) + "-01").AddYears(-1).AddMonths(-1).ToString("yyyyMM");
            string sLastMonth = DateTime.Parse(years.Substring(0, 4) + "-" + years.Substring(4, 2) + "-01").AddMonths(-1).ToString("yyyyMM");
            #region 定义Benchmark表格用于存储各地市的基本情况

            //创建一个名为dt_Benchmark的DataTalbe
            DataTable dt = new DataTable("dt_Benchmark");
            dt.Columns.Add(new DataColumn("BM_ID", typeof(int)));
            dt.Columns.Add(new DataColumn("Years", typeof(string)));
            dt.Columns.Add(new DataColumn("Area_ID", typeof(int)));
            dt.Columns.Add(new DataColumn("Name", typeof(string)));
            dt.Columns.Add(new DataColumn("TotalProportion", typeof(string)));
            dt.Columns.Add(new DataColumn("LastMonthlProportion", typeof(string)));
            dt.Columns.Add(new DataColumn("CumulativProportion", typeof(string)));
            dt.Columns.Add(new DataColumn("AdjustProportion", typeof(string)));
            dt.Columns.Add(new DataColumn("ExpectedRate", typeof(string)));
            dt.Columns.Add(new DataColumn("ActualRate", typeof(string)));
            dt.Columns.Add(new DataColumn("CalculationBenchmark", typeof(string)));
            dt.Columns.Add(new DataColumn("ManuallyBenchmark", typeof(string)));
            dt.Columns.Add(new DataColumn("Bussiness", typeof(string)));

            #endregion
            //获取安徽省下所有地市的标杆值信息
            DataView areadv = SQLHelper.ExecuteDataView("select ID,[Name] from Area where ParentID='10' and ID!='41'");
            //当前月份修正比例计算
            string s2gFlowQ = SQLHelper.ExecuteString("select sum(Value) from rt_2GFlow where substring(Years,1,4) = '" + years.Substring(0, 4) + "' and Area_ID='41'");
            string s3gFlowQ = SQLHelper.ExecuteString("select sum(Value) from rt_3GFlow where substring(Years,1,4) = '" + years.Substring(0, 4) + "' and Area_ID='41'");
            string sPhoneQ = SQLHelper.ExecuteString("select sum(Value*5.85) from rt_Telephone where substring(Years,1,4) = '" + years.Substring(0, 4) + "' and Area_ID='41'");
            string sTDQ = SQLHelper.ExecuteString("select sum(Value*5.49) from rt_TDTraffic where substring(Years,1,4) = '" + years.Substring(0, 4) + "' and Area_ID='41'");
            double dTraffictmpQ = (Double.Parse(String.IsNullOrEmpty(s2gFlowQ) ? "0" : s2gFlowQ) +
                            Double.Parse(String.IsNullOrEmpty(s3gFlowQ) ? "0" : s3gFlowQ) +
                            Double.Parse(String.IsNullOrEmpty(sPhoneQ) ? "0" : sPhoneQ) +
                            Double.Parse(String.IsNullOrEmpty(sTDQ) ? "0" : sTDQ));
            string sPowerQ = SQLHelper.ExecuteString("select sum(Power) from rt_EnergyConsumption where substring(Years,1,4) = '" + years.Substring(0, 4) + "' and Area_ID='41'");
            double dPowerQ = Double.Parse(sPowerQ);
            double dBusinessPowerUnitQ = dPowerQ / dTraffictmpQ * 10000;

            string sBusinessPowerUnit = dBusinessPowerUnitQ.ToString("0.00");
            //sCorrectionRatio 修正比例
            string sCorrectionRatio = SQLHelper.ExecuteString("select MonthRatio from tb_FixedRatio where Years='" + years + "'");
            if (String.IsNullOrEmpty(sCorrectionRatio))
            {
                sErr = "修正比例未录入！";
                return null;
            }
            for (int i = 0; i < areadv.Count; i++)
            {
                DataRow dr = dt.NewRow();
                dr["BM_ID"] = i.ToString();
                dr["Years"] = years;
                dr["Area_ID"] = areadv[i][0].ToString();
                dr["Name"] = areadv[i][1].ToString();
                if (areadv[i][0].ToString() == "39")//省公司
                    continue;
                //1.5月单位业务量能耗占全年比例算法：2009年5月份/2009年全年单位业务量能耗*(2010年全年单位业务量能耗)
                //2010年全年单位业务量能耗 = 2009年全年单位业务量能耗*单位业务量能耗变化率
                //sTotalProportion 单位业务量能耗占全年比例

                #region 本年全年单位业务量能耗 sYearAlarmValue

                string s2gFlow = SQLHelper.ExecuteString("select sum(Value) from rt_2GFlow where substring(Years,1,4) = '" + sLastYears.Substring(0, 4) + "' and Area_ID='" + areadv[i][0].ToString() + "'");
                string s3gFlow = SQLHelper.ExecuteString("select sum(Value) from rt_3GFlow where substring(Years,1,4) = '" + sLastYears.Substring(0, 4) + "' and Area_ID='" + areadv[i][0].ToString() + "'");
                string sPhone = SQLHelper.ExecuteString("select sum(Value*5.85) from rt_Telephone where substring(Years,1,4) = '" + sLastYears.Substring(0, 4) + "' and Area_ID='" + areadv[i][0].ToString() + "'");
                string sTD = SQLHelper.ExecuteString("select sum(Value*5.49) from rt_TDTraffic where substring(Years,1,4) = '" + sLastYears.Substring(0, 4) + "' and Area_ID='" + areadv[i][0].ToString() + "'");
                double dTrafficProvince = Double.Parse(String.IsNullOrEmpty(s2gFlow) ? "0" : s2gFlow) +
                                Double.Parse(String.IsNullOrEmpty(s3gFlow) ? "0" : s3gFlow) +
                                Double.Parse(String.IsNullOrEmpty(sPhone) ? "0" : sPhone) +
                                Double.Parse(String.IsNullOrEmpty(sTD) ? "0" : sTD);
                dTrafficProvince = dTrafficProvince / 10000;
                if (dTrafficProvince == 0)
                {
                    sErr = "业务量为空，请导入相关数据！";
                    return null;
                }
                //往年单位业务量能耗 dBusinessPowerUnit
                string sPowerProvince = SQLHelper.ExecuteString("select sum(Power) from rt_EnergyConsumption where substring(Years,1,4) = '" + sLastYears.Substring(0, 4) + "' and Area_ID='" + areadv[i][0].ToString() + "'");
                if (String.IsNullOrEmpty(sPowerProvince))
                {
                    sErr = "耗电量为空，请导入相关数据！";
                    return null;
                }
                double dPowerProvince = Double.Parse(sPowerProvince);
                double dBusinessPowerUnit = dPowerProvince / dTrafficProvince;
                //2010年全年单位业务量能耗 sYearAlarmValue
                string sBussinessRate = SQLHelper.ExecuteString("select top 1 BusinessRate from tb_ChangeRate where Years='" + sYear.Substring(0, 4) + "'");
                if (String.IsNullOrEmpty(sBussinessRate))
                {
                    sErr = "单位电信业务量变化率为空，请录入相关数据！";
                    return null;
                }
                double dBussinessRate = Double.Parse(sBussinessRate);
                double sYearAlarmValue = (1 - dBussinessRate) * dBusinessPowerUnit;

                #endregion

                #region 本月的单位业务量能耗 dBusinessPowerUnitMonth

                s2gFlow = SQLHelper.ExecuteString("select sum(Value) from rt_2GFlow where Years = '" + sLastYears + "' and Area_ID='" + areadv[i][0].ToString() + "'");
                s3gFlow = SQLHelper.ExecuteString("select sum(Value) from rt_3GFlow where Years = '" + sLastYears + "' and Area_ID='" + areadv[i][0].ToString() + "'");
                sPhone = SQLHelper.ExecuteString("select sum(Value*5.85) from rt_Telephone where Years = '" + sLastYears + "' and Area_ID='" + areadv[i][0].ToString() + "'");
                sTD = SQLHelper.ExecuteString("select sum(Value*5.49) from rt_TDTraffic where Years = '" + sLastYears + "' and Area_ID='" + areadv[i][0].ToString() + "'");
                double dTrafficYears = Double.Parse(String.IsNullOrEmpty(s2gFlow) ? "0" : s2gFlow) +
                                Double.Parse(String.IsNullOrEmpty(s3gFlow) ? "0" : s3gFlow) +
                                Double.Parse(String.IsNullOrEmpty(sPhone) ? "0" : sPhone) +
                                Double.Parse(String.IsNullOrEmpty(sTD) ? "0" : sTD);
                dTrafficYears = dTrafficYears / 10000;
                if (dTrafficYears == 0)
                {
                    sErr = "业务量为空，请导入相关数据！";
                    return null;
                }
                //往年单位业务量能耗 dBusinessPowerUnit
                string sPowerYears = SQLHelper.ExecuteString("select sum(Power) from rt_EnergyConsumption where Years = '" + sLastYears + "' and Area_ID='" + areadv[i][0].ToString() + "'");
                if (String.IsNullOrEmpty(sPowerYears))
                {
                    sErr = "耗电量为空，请导入相关数据！";
                    return null;
                }
                double dPowerYears = Double.Parse(sPowerYears);
                double dBusinessPowerUnitMonth = dPowerYears / dTrafficYears;

                #endregion

                //2010年全年单位业务量能耗 sYearAlarmValue
                double dTotalProportion = dBusinessPowerUnitMonth / dBusinessPowerUnit * sYearAlarmValue;

                //2.5月单位业务量能耗占4月比例算法：2009年5月份/2009年4月份*2010年4月份

                #region 去年同月的单位业务量能耗 dBusinessPowerUnitLastYears

                s2gFlow = SQLHelper.ExecuteString("select sum(Value) from rt_2GFlow where Years = '" + sLastYears + "' and Area_ID='" + areadv[i][0].ToString() + "'");
                s3gFlow = SQLHelper.ExecuteString("select sum(Value) from rt_3GFlow where Years = '" + sLastYears + "' and Area_ID='" + areadv[i][0].ToString() + "'");
                sPhone = SQLHelper.ExecuteString("select sum(Value*5.85) from rt_Telephone where Years = '" + sLastYears + "' and Area_ID='" + areadv[i][0].ToString() + "'");
                sTD = SQLHelper.ExecuteString("select sum(Value*5.49) from rt_TDTraffic where Years = '" + sLastYears + "' and Area_ID='" + areadv[i][0].ToString() + "'");
                double dTrafficLastYears = Double.Parse(String.IsNullOrEmpty(s2gFlow) ? "0" : s2gFlow) +
                                Double.Parse(String.IsNullOrEmpty(s3gFlow) ? "0" : s3gFlow) +
                                Double.Parse(String.IsNullOrEmpty(sPhone) ? "0" : sPhone) +
                                Double.Parse(String.IsNullOrEmpty(sTD) ? "0" : sTD);
                dTrafficLastYears = dTrafficLastYears / 10000;
                if (dTrafficLastYears == 0)
                {
                    sErr = "业务量为空，请导入相关数据！";
                    return null;
                }
                //往年单位业务量能耗 dBusinessPowerUnit
                string sPowerLastYears = SQLHelper.ExecuteString("select sum(Power) from rt_EnergyConsumption where Years = '" + sLastYears + "' and Area_ID='" + areadv[i][0].ToString() + "'");
                if (String.IsNullOrEmpty(sPowerLastYears))
                {
                    sErr = "耗电量为空，请导入相关数据！";
                    return null;
                }
                double dPowerLastYears = Double.Parse(sPowerLastYears);
                double dBusinessPowerUnitLastYears = dPowerLastYears / dTrafficLastYears;

                #endregion

                #region 去年上一个月的单位业务量能耗 dBusinessPowerUnitLastYearsMonth

                s2gFlow = SQLHelper.ExecuteString("select sum(Value) from rt_2GFlow where Years = '" + sLastYearsMonth + "' and Area_ID='" + areadv[i][0].ToString() + "'");
                s3gFlow = SQLHelper.ExecuteString("select sum(Value) from rt_3GFlow where Years = '" + sLastYearsMonth + "' and Area_ID='" + areadv[i][0].ToString() + "'");
                sPhone = SQLHelper.ExecuteString("select sum(Value*5.85) from rt_Telephone where Years = '" + sLastYearsMonth + "' and Area_ID='" + areadv[i][0].ToString() + "'");
                sTD = SQLHelper.ExecuteString("select sum(Value*5.49) from rt_TDTraffic where Years = '" + sLastYearsMonth + "' and Area_ID='" + areadv[i][0].ToString() + "'");
                double dTrafficLastYearsMonths = Double.Parse(String.IsNullOrEmpty(s2gFlow) ? "0" : s2gFlow) +
                                Double.Parse(String.IsNullOrEmpty(s3gFlow) ? "0" : s3gFlow) +
                                Double.Parse(String.IsNullOrEmpty(sPhone) ? "0" : sPhone) +
                                Double.Parse(String.IsNullOrEmpty(sTD) ? "0" : sTD);
                dTrafficLastYearsMonths = dTrafficLastYearsMonths / 10000;
                if (dTrafficLastYearsMonths == 0)
                {
                    sErr = "业务量为空，请导入相关数据！";
                    return null;
                }
                //往年单位业务量能耗 dBusinessPowerUnit
                string sPowerLastYearsMonths = SQLHelper.ExecuteString("select sum(Power) from rt_EnergyConsumption where Years = '" + sLastYearsMonth + "' and Area_ID='" + areadv[i][0].ToString() + "'");
                if (String.IsNullOrEmpty(sPowerLastYearsMonths))
                {
                    sErr = "耗电量为空，请导入相关数据！";
                    return null;
                }
                double dPowerLastYearsMonths = Double.Parse(sPowerLastYearsMonths);
                double dBusinessPowerUnitLastYearsMonth = dPowerLastYearsMonths / dTrafficLastYearsMonths;

                #endregion

                #region 上一个月的单位业务量能耗 dBusinessPowerUnitLastMonth

                s2gFlow = SQLHelper.ExecuteString("select sum(Value) from rt_2GFlow where Years = '" + sLastMonth + "' and Area_ID='" + areadv[i][0].ToString() + "'");
                s3gFlow = SQLHelper.ExecuteString("select sum(Value) from rt_3GFlow where Years = '" + sLastMonth + "' and Area_ID='" + areadv[i][0].ToString() + "'");
                sPhone = SQLHelper.ExecuteString("select sum(Value*5.85) from rt_Telephone where Years = '" + sLastMonth + "' and Area_ID='" + areadv[i][0].ToString() + "'");
                sTD = SQLHelper.ExecuteString("select sum(Value*5.49) from rt_TDTraffic where Years = '" + sLastMonth + "' and Area_ID='" + areadv[i][0].ToString() + "'");
                double dTrafficLastMonth = Double.Parse(String.IsNullOrEmpty(s2gFlow) ? "0" : s2gFlow) +
                                Double.Parse(String.IsNullOrEmpty(s3gFlow) ? "0" : s3gFlow) +
                                Double.Parse(String.IsNullOrEmpty(sPhone) ? "0" : sPhone) +
                                Double.Parse(String.IsNullOrEmpty(sTD) ? "0" : sTD);
                dTrafficLastMonth = dTrafficLastMonth / 10000;
                if (dTrafficLastMonth == 0)
                {
                    sErr = "业务量为空，请导入相关数据！";
                    return null;
                }
                //往年单位业务量能耗 dBusinessPowerUnit
                string sPowerLastMonth = SQLHelper.ExecuteString("select sum(Power) from rt_EnergyConsumption where Years = '" + sLastMonth + "' and Area_ID='" + areadv[i][0].ToString() + "'");
                if (String.IsNullOrEmpty(sPowerLastMonth))
                {
                    sErr = "耗电量为空，请导入相关数据！";
                    return null;
                }
                double dPowerLastMonth = Double.Parse(sPowerLastMonth);
                double dBusinessPowerUnitLastMonth = dPowerLastMonth / dTrafficLastMonth;

                #endregion

                double dLastMonthlProportion = dBusinessPowerUnitLastYears / dBusinessPowerUnitLastYearsMonth * dBusinessPowerUnitLastMonth;

                //3.5月单位业务量能耗占（1-4）月比例算法：2009年5月份/2009年1月至4月份*2010年1月至4月份

                #region 去年上一个月的单位业务量能耗累计（1-x）月  dBusinessPowerUnitLastYearsMonthLJ

                s2gFlow = SQLHelper.ExecuteString("select sum(Value) from rt_2GFlow where Years>='" + sLastYearsMonth.Substring(0, 4) + "01' and Years <= '" + sLastYearsMonth + "' and Area_ID='" + areadv[i][0].ToString() + "'");
                s3gFlow = SQLHelper.ExecuteString("select sum(Value) from rt_3GFlow where Years>='" + sLastYearsMonth.Substring(0, 4) + "01' and Years <= '" + sLastYearsMonth + "' and Area_ID='" + areadv[i][0].ToString() + "'");
                sPhone = SQLHelper.ExecuteString("select sum(Value*5.85) from rt_Telephone where Years>='" + sLastYearsMonth.Substring(0, 4) + "01' and Years <= '" + sLastYearsMonth + "' and Area_ID='" + areadv[i][0].ToString() + "'");
                sTD = SQLHelper.ExecuteString("select sum(Value*5.49) from rt_TDTraffic where Years>='" + sLastYearsMonth.Substring(0, 4) + "01' and Years <= '" + sLastYearsMonth + "' and Area_ID='" + areadv[i][0].ToString() + "'");
                double dTraffictmp = Double.Parse(String.IsNullOrEmpty(s2gFlow) ? "0" : s2gFlow) +
                                Double.Parse(String.IsNullOrEmpty(s3gFlow) ? "0" : s3gFlow) +
                                Double.Parse(String.IsNullOrEmpty(sPhone) ? "0" : sPhone) +
                                Double.Parse(String.IsNullOrEmpty(sTD) ? "0" : sTD);
                dTraffictmp = dTraffictmp / 10000;
                if (dTraffictmp == 0)
                {
                    sErr = "业务量为空，请导入相关数据！";
                    return null;
                }
                //往年单位业务量能耗 dBusinessPowerUnit
                string sPower = SQLHelper.ExecuteString("select sum(Power) from rt_EnergyConsumption where Years>='" + sLastYearsMonth.Substring(0, 4) + "01' and Years <= '" + sLastYearsMonth + "' and Area_ID='" + areadv[i][0].ToString() + "'");
                if (String.IsNullOrEmpty(sPower))
                {
                    sErr = "耗电量为空，请导入相关数据！";
                    return null;
                }
                double dPower = Double.Parse(sPower);
                double dBusinessPowerUnitLastYearsMonthLJ = dPower / dTraffictmp;

                #endregion

                #region 上一个月的单位业务量能耗累计（1-x）月  dBusinessPowerUnitLastMonthLJ

                s2gFlow = SQLHelper.ExecuteString("select sum(Value) from rt_2GFlow where Years>='" + sLastMonth.Substring(0, 4) + "01' and Years <= '" + sLastMonth + "' and Area_ID='" + areadv[i][0].ToString() + "'");
                s3gFlow = SQLHelper.ExecuteString("select sum(Value) from rt_3GFlow where Years>='" + sLastMonth.Substring(0, 4) + "01' and Years <= '" + sLastMonth + "' and Area_ID='" + areadv[i][0].ToString() + "'");
                sPhone = SQLHelper.ExecuteString("select sum(Value*5.85) from rt_Telephone where Years>='" + sLastMonth.Substring(0, 4) + "01' and Years <= '" + sLastMonth + "' and Area_ID='" + areadv[i][0].ToString() + "'");
                sTD = SQLHelper.ExecuteString("select sum(Value*5.49) from rt_TDTraffic where Years>='" + sLastMonth.Substring(0, 4) + "01' and Years <= '" + sLastMonth + "' and Area_ID='" + areadv[i][0].ToString() + "'");
                dTraffictmp = Double.Parse(String.IsNullOrEmpty(s2gFlow) ? "0" : s2gFlow) +
                                Double.Parse(String.IsNullOrEmpty(s3gFlow) ? "0" : s3gFlow) +
                                Double.Parse(String.IsNullOrEmpty(sPhone) ? "0" : sPhone) +
                                Double.Parse(String.IsNullOrEmpty(sTD) ? "0" : sTD);
                dTraffictmp = dTraffictmp / 10000;
                if (dTraffictmp == 0)
                {
                    sErr = "业务量为空，请导入相关数据！";
                    return null;
                }
                //往年单位业务量能耗 dBusinessPowerUnit
                sPower = SQLHelper.ExecuteString("select sum(Power) from rt_EnergyConsumption where Years>='" + sLastMonth.Substring(0, 4) + "01' and Years <= '" + sLastMonth + "' and Area_ID='" + areadv[i][0].ToString() + "'");
                if (String.IsNullOrEmpty(sPower))
                {
                    sErr = "耗电量为空，请导入相关数据！";
                    return null;
                }
                dPower = Double.Parse(sPower);
                double dBusinessPowerUnitLastMonthLJ = dPower / dTraffictmp;

                #endregion

                double dCumulativProportion = dBusinessPowerUnitLastYears / dBusinessPowerUnitLastYearsMonthLJ * dBusinessPowerUnitLastMonthLJ;
                //4.警戒线月度调整比例算法：2009年全年耗电量*(1+耗电量变化率)*修正比例（5月）
                double sAdjustProportion = sYearAlarmValue * double.Parse(sCorrectionRatio);
                //5.5月预计/4月耗电变化率：(5月份预测耗电量/4月份预测耗电量)-1
                double sExpectedRate = dPowerYears / dPowerLastMonth - 1;
                //6.5月/4月业务量变化率：（5月份业务量/4月份业务量）-1
                double dActualRate = dTrafficYears / dTrafficLastMonth - 1;
                //7.计算标杆值
                DataView wight = SQLHelper.ExecuteDataView("select * from tb_Weight where Years='" + years.Substring(0, 4) + "'");
                if (wight.Count == 0)
                {
                    sErr = "权重未设置！";
                    return null;
                }
                else
                {
                    string w1 = wight[0]["Weight1"].ToString();
                    string w2 = wight[0]["Weight2"].ToString();
                    string w3 = wight[0]["Weight3"].ToString();
                    string w4 = wight[0]["Weight4"].ToString();
                    double dCalculationBenchmark = dTotalProportion * double.Parse(w1) + dLastMonthlProportion * double.Parse(w2) + dCumulativProportion * double.Parse(w3) + sAdjustProportion * double.Parse(w4);
                    dr["TotalProportion"] = dTotalProportion.ToString("0.00");
                    dr["LastMonthlProportion"] = dLastMonthlProportion.ToString("0.00");
                    dr["CumulativProportion"] = dCumulativProportion.ToString("0.00");
                    dr["AdjustProportion"] = sAdjustProportion.ToString("0.00");
                    dr["ExpectedRate"] = (sExpectedRate * 100).ToString("0.0") + "%";
                    dr["ActualRate"] = (dActualRate * 100).ToString("0.0") + "%";
                    dr["CalculationBenchmark"] = dCalculationBenchmark.ToString("0.00");
                    dr["ManuallyBenchmark"] = SQLHelper.ExecuteString("select MarkValue from tb_ForecastBusinessNew where Area_ID='" + areadv[i][0].ToString() + "'and Years='" + years + "'");
                    dr["Bussiness"] = dBusinessPowerUnitLastMonth.ToString("0.0");
                    dt.Rows.Add(dr);
                }
            }
            return dt.DefaultView;
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataView GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select BM_ID,TotalProportion,LastMonthlProportion,CumulativProportion,AdjustProportion,ExpectedRate,ActualRate,CalculationBenchmark,ManuallyBenchmark ");
            strSql.Append(" FROM tb_Benchmark ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return SQLHelper.ExecuteDataView(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataView GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" BM_ID,TotalProportion,LastMonthlProportion,CumulativProportion,AdjustProportion,ExpectedRate,ActualRate,CalculationBenchmark,ManuallyBenchmark ");
            strSql.Append(" FROM tb_Benchmark ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return SQLHelper.ExecuteDataView(strSql.ToString());
        }

        #endregion  成员方法

        public DataView GetBenchmarkListB(string years, ref string sErr)
        {
            string sLastYears = DateTime.Parse(years.Substring(0, 4) + "-" + years.Substring(4, 2) + "-01").AddYears(-1).ToString("yyyyMM");
            string sYear = years.Substring(0, 4);
            string sLastYearsMonth = DateTime.Parse(years.Substring(0, 4) + "-" + years.Substring(4, 2) + "-01").AddYears(-1).AddMonths(-1).ToString("yyyyMM");
            string sLastMonth = DateTime.Parse(years.Substring(0, 4) + "-" + years.Substring(4, 2) + "-01").AddMonths(-1).ToString("yyyyMM");
            #region 定义Benchmark表格用于存储各地市的基本情况

            //创建一个名为dt_Benchmark的DataTalbe
            DataTable dt = new DataTable("dt_Benchmark");
            dt.Columns.Add(new DataColumn("BM_ID", typeof(int)));
            dt.Columns.Add(new DataColumn("Years", typeof(string)));
            dt.Columns.Add(new DataColumn("Area_ID", typeof(int)));
            dt.Columns.Add(new DataColumn("Name", typeof(string)));
            dt.Columns.Add(new DataColumn("TotalProportion", typeof(string)));
            dt.Columns.Add(new DataColumn("LastMonthlProportion", typeof(string)));
            dt.Columns.Add(new DataColumn("CumulativProportion", typeof(string)));
            dt.Columns.Add(new DataColumn("AdjustProportion", typeof(string)));
            dt.Columns.Add(new DataColumn("ExpectedRate", typeof(string)));
            dt.Columns.Add(new DataColumn("ActualRate", typeof(string)));
            dt.Columns.Add(new DataColumn("CalculationBenchmark", typeof(string)));
            dt.Columns.Add(new DataColumn("ManuallyBenchmark", typeof(string)));
            //------------------2011.3.31 增加单位业务量能耗--------------------------//
            dt.Columns.Add(new DataColumn("Bussiness", typeof(string)));
            //------------------2011.3.31 增加单位业务量能耗--------------------------//

            #endregion
            //获取安徽省下所有地市的标杆值信息
            DataView areadv = SQLHelper.ExecuteDataView("select ID,[Name] from Area where ParentID='10' and ID!='41'");
            //当前月份修正比例计算
            string sBusinessPowerUnit = SQLHelper.ExecuteString("select cast((select sum(cast(Power as decimal(20, 4))) from rt_EnergyConsumption where Years = '" + years + "') as decimal(20, 4))/cast((select sum(cast(Value as decimal(20, 4))) from rt_TDTraffic where Years ='" + years + "')as decimal(20, 4))");
            //sCorrectionRatio 修正比例
            string sCorrectionRatio = SQLHelper.ExecuteString("select MonthRatio from tb_FixedRatio where Years='" + years + "'");
            if (String.IsNullOrEmpty(sCorrectionRatio))
            {
                sErr = "修正比例未录入！";
                return null;
            }
            for (int i = 0; i < areadv.Count; i++)
            {
                DataRow dr = dt.NewRow();
                dr["BM_ID"] = i.ToString();
                dr["Years"] = years;
                dr["Area_ID"] = areadv[i][0].ToString();
                dr["Name"] = areadv[i][1].ToString();
                if (areadv[i][0].ToString() == "39")//省公司
                    continue;
                //1.5月单位业务量能耗占全年比例算法：2009年5月份/2009年全年单位业务量能耗*(2010年全年单位业务量能耗)
                //2010年全年单位业务量能耗 = 2009年全年单位业务量能耗*单位业务量能耗变化率
                //sTotalProportion 单位业务量能耗占全年比例

                #region 本年全年单位业务量能耗 sYearAlarmValue

                string sPDCH = SQLHelper.ExecuteString("select sum(Value) from rt_PDCH where substring(Years,1,4) = '" + sLastYears.Substring(0, 4) + "' and Area_ID='41'");
                string sPhone = SQLHelper.ExecuteString("select sum(Value) from rt_Telephone where substring(Years,1,4) = '" + sLastYears.Substring(0, 4) + "' and Area_ID='41'");
                string sTD = SQLHelper.ExecuteString("select sum(Value) from rt_TDTraffic where substring(Years,1,4) = '" + sLastYears.Substring(0, 4) + "' and Area_ID='41'");
                double dTrafficProvince = Double.Parse(String.IsNullOrEmpty(sPDCH) ? "0" : sPDCH) +
                                Double.Parse(String.IsNullOrEmpty(sPhone) ? "0" : sPhone) +
                                Double.Parse(String.IsNullOrEmpty(sTD) ? "0" : sTD);
                dTrafficProvince = dTrafficProvince / 10000;
                if (dTrafficProvince == 0)
                {
                    sErr = "业务量为空，请导入相关数据！";
                    return null;
                }
                //往年单位业务量能耗 dBusinessPowerUnit
                string sPowerProvince = SQLHelper.ExecuteString("select sum(Power) from rt_EnergyConsumption where substring(Years,1,4) = '" + sLastYears.Substring(0, 4) + "' and Area_ID='41'");
                if (String.IsNullOrEmpty(sPowerProvince))
                {
                    sErr = "耗电量为空，请导入相关数据！";
                    return null;
                }
                double dPowerProvince = Double.Parse(sPowerProvince);
                double dBusinessPowerUnit = dPowerProvince / dTrafficProvince;
                //2010年全年单位业务量能耗 sYearAlarmValue
                string sBussinessRate = SQLHelper.ExecuteString("select top 1 BusinessRate from tb_ChangeRate where Years='" + sYear.Substring(0, 4) + "'");
                if (String.IsNullOrEmpty(sBussinessRate))
                {
                    sErr = "单位电信业务量变化率为空，请录入相关数据！";
                    return null;
                }
                double dBussinessRate = Double.Parse(sBussinessRate);
                double sYearAlarmValue = (1 + dBussinessRate) * dBusinessPowerUnit;

                #endregion

                #region 本月的单位业务量能耗 dBusinessPowerUnitMonth

                sPDCH = SQLHelper.ExecuteString("select sum(Value) from rt_PDCH where Years = '" + years + "' and Area_ID='" + areadv[i][0].ToString() + "'");
                sPhone = SQLHelper.ExecuteString("select sum(Value*5.85) from rt_Telephone where Years = '" + years + "' and Area_ID='" + areadv[i][0].ToString() + "'");
                sTD = SQLHelper.ExecuteString("select sum(Value*5.49) from rt_TDTraffic where Years = '" + years + "' and Area_ID='" + areadv[i][0].ToString() + "'");
                double dTrafficYears = Double.Parse(String.IsNullOrEmpty(sPDCH) ? "0" : sPDCH) +
                                Double.Parse(String.IsNullOrEmpty(sPhone) ? "0" : sPhone) +
                                Double.Parse(String.IsNullOrEmpty(sTD) ? "0" : sTD);
                dTrafficYears = dTrafficYears / 10000;
                if (dTrafficYears == 0)
                {
                    sErr = "业务量为空，请导入相关数据！";
                    return null;
                }
                //往年单位业务量能耗 dBusinessPowerUnit
                string sPowerYears = SQLHelper.ExecuteString("select sum(Power) from rt_EnergyConsumption where Years = '" + years + "' and Area_ID='" + areadv[i][0].ToString() + "'");
                if (String.IsNullOrEmpty(sPowerYears))
                {
                    sErr = "耗电量为空，请导入相关数据！";
                    return null;
                }
                double dPowerYears = Double.Parse(sPowerYears);
                double dBusinessPowerUnitMonth = dPowerYears / dTrafficYears;

                #endregion

                //2010年全年单位业务量能耗 sYearAlarmValue
                double dTotalProportion = dBusinessPowerUnitMonth / dBusinessPowerUnit * sYearAlarmValue;

                //2.5月单位业务量能耗占4月比例算法：2009年5月份/2009年4月份*2010年4月份

                #region 去年同月的单位业务量能耗 dBusinessPowerUnitLastYears

                sPDCH = SQLHelper.ExecuteString("select sum(Value) from rt_PDCH where Years = '" + sLastYears + "' and Area_ID='" + areadv[i][0].ToString() + "'");
                sPhone = SQLHelper.ExecuteString("select sum(Value*5.85) from rt_Telephone where Years = '" + sLastYears + "' and Area_ID='" + areadv[i][0].ToString() + "'");
                sTD = SQLHelper.ExecuteString("select sum(Value*5.49) from rt_TDTraffic where Years = '" + sLastYears + "' and Area_ID='" + areadv[i][0].ToString() + "'");
                double dTrafficLastYears = Double.Parse(String.IsNullOrEmpty(sPDCH) ? "0" : sPDCH) +
                                Double.Parse(String.IsNullOrEmpty(sPhone) ? "0" : sPhone) +
                                Double.Parse(String.IsNullOrEmpty(sTD) ? "0" : sTD);
                dTrafficLastYears = dTrafficLastYears / 10000;
                if (dTrafficLastYears == 0)
                {
                    sErr = "业务量为空，请导入相关数据！";
                    return null;
                }
                //往年单位业务量能耗 dBusinessPowerUnit
                string sPowerLastYears = SQLHelper.ExecuteString("select sum(Power) from rt_EnergyConsumption where Years = '" + sLastYears + "' and Area_ID='" + areadv[i][0].ToString() + "'");
                if (String.IsNullOrEmpty(sPowerLastYears))
                {
                    sErr = "耗电量为空，请导入相关数据！";
                    return null;
                }
                double dPowerLastYears = Double.Parse(sPowerLastYears);
                double dBusinessPowerUnitLastYears = dPowerLastYears / dTrafficLastYears;

                #endregion

                #region 去年上一个月的单位业务量能耗 dBusinessPowerUnitLastYearsMonth

                sPDCH = SQLHelper.ExecuteString("select sum(Value) from rt_PDCH where Years = '" + sLastYearsMonth + "' and Area_ID='" + areadv[i][0].ToString() + "'");
                sPhone = SQLHelper.ExecuteString("select sum(Value*5.85) from rt_Telephone where Years = '" + sLastYearsMonth + "' and Area_ID='" + areadv[i][0].ToString() + "'");
                sTD = SQLHelper.ExecuteString("select sum(Value*5.49) from rt_TDTraffic where Years = '" + sLastYearsMonth + "' and Area_ID='" + areadv[i][0].ToString() + "'");
                double dTrafficLastYearsMonths = Double.Parse(String.IsNullOrEmpty(sPDCH) ? "0" : sPDCH) +
                                Double.Parse(String.IsNullOrEmpty(sPhone) ? "0" : sPhone) +
                                Double.Parse(String.IsNullOrEmpty(sTD) ? "0" : sTD);
                dTrafficLastYearsMonths = dTrafficLastYearsMonths / 10000;
                if (dTrafficLastYearsMonths == 0)
                {
                    sErr = "业务量为空，请导入相关数据！";
                    return null;
                }
                //往年单位业务量能耗 dBusinessPowerUnit
                string sPowerLastYearsMonths = SQLHelper.ExecuteString("select sum(Power) from rt_EnergyConsumption where Years = '" + sLastYearsMonth + "' and Area_ID='" + areadv[i][0].ToString() + "'");
                if (String.IsNullOrEmpty(sPowerLastYearsMonths))
                {
                    sErr = "耗电量为空，请导入相关数据！";
                    return null;
                }
                double dPowerLastYearsMonths = Double.Parse(sPowerLastYearsMonths);
                double dBusinessPowerUnitLastYearsMonth = dPowerLastYearsMonths / dTrafficLastYearsMonths;

                #endregion

                #region 上一个月的单位业务量能耗 dBusinessPowerUnitLastMonth

                sPDCH = SQLHelper.ExecuteString("select sum(Value) from rt_PDCH where Years = '" + sLastMonth + "' and Area_ID='" + areadv[i][0].ToString() + "'");
                sPhone = SQLHelper.ExecuteString("select sum(Value*5.85) from rt_Telephone where Years = '" + sLastMonth + "' and Area_ID='" + areadv[i][0].ToString() + "'");
                sTD = SQLHelper.ExecuteString("select sum(Value*5.49) from rt_TDTraffic where Years = '" + sLastMonth + "' and Area_ID='" + areadv[i][0].ToString() + "'");
                double dTrafficLastMonth = Double.Parse(String.IsNullOrEmpty(sPDCH) ? "0" : sPDCH) +
                                Double.Parse(String.IsNullOrEmpty(sPhone) ? "0" : sPhone) +
                                Double.Parse(String.IsNullOrEmpty(sTD) ? "0" : sTD);
                dTrafficLastMonth = dTrafficLastMonth / 10000;
                if (dTrafficLastMonth == 0)
                {
                    sErr = "业务量为空，请导入相关数据！";
                    return null;
                }
                //往年单位业务量能耗 dBusinessPowerUnit
                string sPowerLastMonth = SQLHelper.ExecuteString("select sum(Power) from rt_EnergyConsumption where Years = '" + sLastMonth + "' and Area_ID='" + areadv[i][0].ToString() + "'");
                if (String.IsNullOrEmpty(sPowerLastMonth))
                {
                    sErr = "耗电量为空，请导入相关数据！";
                    return null;
                }
                double dPowerLastMonth = Double.Parse(sPowerLastMonth);
                double dBusinessPowerUnitLastMonth = dPowerLastMonth / dTrafficLastMonth;

                #endregion

                double dLastMonthlProportion = dBusinessPowerUnitLastYears / dBusinessPowerUnitLastYearsMonth * dBusinessPowerUnitLastMonth;

                //3.5月单位业务量能耗占（1-4）月比例算法：2009年5月份/2009年1月至4月份*2010年1月至4月份

                #region 去年上一个月的单位业务量能耗累计（1-x）月  dBusinessPowerUnitLastYearsMonthLJ

                sPDCH = SQLHelper.ExecuteString("select sum(Value) from rt_PDCH where Years>='" + sLastYearsMonth.Substring(0, 4) + "01' and Years <= '" + sLastYearsMonth + "' and Area_ID='" + areadv[i][0].ToString() + "'");
                sPhone = SQLHelper.ExecuteString("select sum(Value*5.85) from rt_Telephone where Years>='" + sLastYearsMonth.Substring(0, 4) + "01' and Years <= '" + sLastYearsMonth + "' and Area_ID='" + areadv[i][0].ToString() + "'");
                sTD = SQLHelper.ExecuteString("select sum(Value*5.49) from rt_TDTraffic where Years>='" + sLastYearsMonth.Substring(0, 4) + "01' and Years <= '" + sLastYearsMonth + "' and Area_ID='" + areadv[i][0].ToString() + "'");
                double dTraffictmp = Double.Parse(String.IsNullOrEmpty(sPDCH) ? "0" : sPDCH) +
                                Double.Parse(String.IsNullOrEmpty(sPhone) ? "0" : sPhone) +
                                Double.Parse(String.IsNullOrEmpty(sTD) ? "0" : sTD);
                dTraffictmp = dTraffictmp / 10000;
                if (dTraffictmp == 0)
                {
                    sErr = "业务量为空，请导入相关数据！";
                    return null;
                }
                //往年单位业务量能耗 dBusinessPowerUnit
                string sPower = SQLHelper.ExecuteString("select sum(Power) from rt_EnergyConsumption where Years>='" + sLastYearsMonth.Substring(0, 4) + "01' and Years <= '" + sLastYearsMonth + "' and Area_ID='" + areadv[i][0].ToString() + "'");
                if (String.IsNullOrEmpty(sPower))
                {
                    sErr = "耗电量为空，请导入相关数据！";
                    return null;
                }
                double dPower = Double.Parse(sPower);
                double dBusinessPowerUnitLastYearsMonthLJ = dPower / dTraffictmp;

                #endregion

                #region 上一个月的单位业务量能耗累计（1-x）月  dBusinessPowerUnitLastMonthLJ

                sPDCH = SQLHelper.ExecuteString("select sum(Value) from rt_PDCH where Years>='" + sLastMonth.Substring(0, 4) + "01' and Years <= '" + sLastMonth + "' and Area_ID='" + areadv[i][0].ToString() + "'");
                sPhone = SQLHelper.ExecuteString("select sum(Value*5.85) from rt_Telephone where Years>='" + sLastMonth.Substring(0, 4) + "01' and Years <= '" + sLastMonth + "' and Area_ID='" + areadv[i][0].ToString() + "'");
                sTD = SQLHelper.ExecuteString("select sum(Value*5.49) from rt_TDTraffic where Years>='" + sLastMonth.Substring(0, 4) + "01' and Years <= '" + sLastMonth + "' and Area_ID='" + areadv[i][0].ToString() + "'");
                dTraffictmp = Double.Parse(String.IsNullOrEmpty(sPDCH) ? "0" : sPDCH) +
                                Double.Parse(String.IsNullOrEmpty(sPhone) ? "0" : sPhone) +
                                Double.Parse(String.IsNullOrEmpty(sTD) ? "0" : sTD);
                dTraffictmp = dTraffictmp / 10000;
                if (dTraffictmp == 0)
                {
                    sErr = "业务量为空，请导入相关数据！";
                    return null;
                }
                //往年单位业务量能耗 dBusinessPowerUnit
                sPower = SQLHelper.ExecuteString("select sum(Power) from rt_EnergyConsumption where Years>='" + sLastMonth.Substring(0, 4) + "01' and Years <= '" + sLastMonth + "' and Area_ID='" + areadv[i][0].ToString() + "'");
                if (String.IsNullOrEmpty(sPower))
                {
                    sErr = "耗电量为空，请导入相关数据！";
                    return null;
                }
                dPower = Double.Parse(sPower);
                double dBusinessPowerUnitLastMonthLJ = dPower / dTraffictmp;

                #endregion

                double dCumulativProportion = dBusinessPowerUnitLastYears / dBusinessPowerUnitLastYearsMonthLJ * dBusinessPowerUnitLastMonthLJ;
                //4.警戒线月度调整比例算法：2009年全年耗电量*(1+耗电量变化率)*修正比例（5月）
                double sAdjustProportion = sYearAlarmValue * double.Parse(sCorrectionRatio);
                //5.5月预计/4月耗电变化率：(5月份预测耗电量/4月份预测耗电量)-1
                double sExpectedRate = dPowerYears / dPowerLastMonth - 1;
                //6.5月/4月业务量变化率：（5月份业务量/4月份业务量）-1
                double dActualRate = dTrafficYears / dTrafficLastMonth - 1;
                //7.计算标杆值
                DataView wight = SQLHelper.ExecuteDataView("select * from tb_Weight where Years='" + years.Substring(0, 4) + "'");
                if (wight.Count == 0)
                {
                    sErr = "权重未设置！";
                    return null;
                }
                else
                {
                    string w1 = wight[0]["Weight1"].ToString();
                    string w2 = wight[0]["Weight2"].ToString();
                    string w3 = wight[0]["Weight3"].ToString();
                    string w4 = wight[0]["Weight4"].ToString();
                    double dCalculationBenchmark = dTotalProportion * double.Parse(w1) + dLastMonthlProportion * double.Parse(w2) + dCumulativProportion * double.Parse(w2) + sAdjustProportion * double.Parse(w2);
                    dr["TotalProportion"] = dTotalProportion.ToString("0.00");
                    dr["LastMonthlProportion"] = dLastMonthlProportion.ToString("0.00");
                    dr["CumulativProportion"] = dCumulativProportion.ToString("0.00");
                    dr["AdjustProportion"] = sAdjustProportion.ToString("0.00");
                    dr["ExpectedRate"] = sExpectedRate.ToString("0.00");
                    dr["ActualRate"] = dActualRate.ToString("0.00");
                    dr["CalculationBenchmark"] = dCalculationBenchmark.ToString("0.00");
                    dr["ManuallyBenchmark"] = SQLHelper.ExecuteString("select MarkValue from tb_ForecastBusinessNew where Area_ID='" + areadv[i][0].ToString() + "'and Years='" + years + "'");
                    //------------------2011.3.31 增加单位业务量能耗--------------------------//
                    dr["Bussiness"] = dBusinessPowerUnitLastMonth.ToString("0.00");
                    //------------------2011.3.31 增加单位业务量能耗--------------------------//
                    dt.Rows.Add(dr);
                }
            }
            return dt.DefaultView;
        }
    }
}

